﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class FM_Clef : FM_BaseNote
{
    public FM_ClefValue clef;

    public FM_Clef(FM_ScoreBase score, FM_ClefValue clef, int staff) : base(FM_NoteType.CLEF, score)
    {
        this.clef = clef;
        this.stave = staff;
    }

    public FM_Clef(FM_ScoreBase score, FM_ClefValue clef, int staff, Color color) : base(FM_NoteType.CLEF, score)
    {
        this.clef = clef;
        this.stave = staff;
        this.color = color;
    }

    public override float GetDisplacement()
    {
        if (clef == FM_ClefValue.TREBLE) return 3.0f;
        else if (clef == FM_ClefValue.BASS) return 1.0f;
        return 1.0f;
    }

    public override string AsString()
    {
        if (clef == FM_ClefValue.BASS) return FM_Const.BassClef;
        if (clef == FM_ClefValue.TREBLE) return FM_Const.TrebleClef;
        return string.Empty;
    }

    private float LineSpan()
    {
        if (clef == FM_ClefValue.BASS) return 3.5f;
        if (clef == FM_ClefValue.TREBLE) return 7;
        return 1;
    }

    public override float WidthAccidental()
    {
        return 0;
    }

    public override float WidthNoteNoStem()
    {
        if (score.score == null) return 0;
        FM_Const.AdjustFont(score.score, FM_Const._4, 2);
        float w = score.score.Font.MeasureText(FM_Const.TrebleClef) + 2 * FM_Const.dpTOpx(FM_Const.DEFAULT_EXTRA_PADDING);
        float w1 = score.score.Font.MeasureText(FM_Const.BassClef) + 2 * FM_Const.dpTOpx(FM_Const.DEFAULT_EXTRA_PADDING);
        return Mathf.Max(w, w1);
    }

    public override float WidthNote()
    {
        return WidthNoDotNoStem();
    }

    public override float WidthDot()
    {
        return 0;
    }

    private float BottomMargin()
    {
        if (score.score == null) return 0;
        FM_Const.AdjustFont(score.score, AsString(), LineSpan());
        Rect bounds = new Rect();
        string s = AsString();
        score.score.Font.GetMaxCharacterBounds(s, 0, s.Length, out bounds);
        return bounds.y;
    }

    private float TopMargin()
    {
        if (score.score == null) return 0;
        FM_Const.AdjustFont(score.score, AsString(), LineSpan());
        Rect bounds = new Rect();
        string s = AsString();
        score.score.Font.GetMaxCharacterBounds(s, 0, s.Length, out bounds);
        return bounds.height;
    }

    public override void DrawNote(ScoreCanvas canvas)
    {
        if (score.score == null) return;
        if (!IsVisible()) return;
        base.DrawNote(canvas);
        score.score.Font.Color = new Color(0,0,0);// new Color32((byte)(color >> 16 & 0xFF), (byte)(color >> 8 & 0xFF), (byte)(color & 0xFF), (byte)(color >> 24 & 0xFF));
        FM_Const.AdjustFont(score.score, FM_Const._4, 2);
        canvas.DrawText(AsString(), StartX + paddingLeft, StartY1 + GetDisplacement() * score.score.GetDistanceBetweenStaveLines(), score.score.Font);
    }

    public override float Left()
    {
        return StartX;
    }

    public override float Bottom()
    {
        if (score.score == null) return 0;
        return StartY1 + GetDisplacement() * score.score.GetDistanceBetweenStaveLines() + BottomMargin();
    }

    public override float Right()
    {
        return StartX + paddingLeft + Width();
    }

    public override float Top()
    {
        if (score.score == null) return 0;
        return StartY1 + GetDisplacement() * score.score.GetDistanceBetweenStaveLines() + TopMargin();
    }
}
